<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				fileList: [],
				fileName: '',
				fileSaveName: '',
				files: [],
				fileKey: "37",
				dir: "",
				categoryCache:'mar-xs-tsalesContractReview-category-cache',
				userListCache:"mar-xs-tsalesContractReview-userlist-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			getDir() {
				let _that = this
				_that.om.request(
					"uploadFiles/getDir", {
						fileKey: _that.fileKey, //
						fileName: _that.fileUrl //保存后文件名
					}
				).then(res => {
					_that.formdata.dir = res.value
				})
			},
			formSubmit(){
				let _that = this
				_that.om.formSave("tsalesContractReview/addTsalesContractReviewByH5",_that.formdata,_that)
			},
			verifyContractFreight(val){
				let _that = this
				if(_that.$refs.autoform.formdata.contractMoney){
					_that.$refs.autoform.formdata.contractFreightShare = (Number(val)/Number(_that.$refs.autoform.formdata.contractMoney))*100+"%"
				}
			},
			verifyContractMoney(val){
				let _that = this
				if(_that.$refs.autoform.formdata.contractFreight){
					_that.$refs.autoform.formdata.contractFreightShare = (Number(_that.$refs.autoform.formdata.contractFreight)/Number(val))*100+"%"
				}
				_that.$refs.autoform.changeEditor("title1","contractFreight","type","numberbox")
			},
			
			initContractFlowName(num){
				let _that = this
				_that.om.request(
					"approveFlowBaseToOrg/getImportantDict",
					{
						formCode: _that.busCode,
						num: num
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.formdata.contractFlowName = res.value.dicname
				})
			},
			verifyContractReduceRate(val){
				let _that = this
				let editor = _that.$refs.autoform.getEditor("title1","contractReduceRate")
				if(Number(val)>Number(editor.max)){
					_that.$refs.autoform.formdata.contractReduceRate = 1
				}
				_that.initContractFlowName(_that.$refs.autoform.formdata.contractReduceRate)
			},
			onSelectContractPos(item){
				let _that = this
				_that.$refs.autoform.formdata.contractInstName = item.instName
			},
			initPosList(userid){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/findPosByUserIdAndInstId",
					{
						userid: userid
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title1","contractPosId","data",configSelectData(res,'id',"posname"))
					_that.$refs.autoform.initSelectIndex("title1","contractPosId")
				})
			},
			initLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.dictZeroComboboxUrl("37970")
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"tsalesContractReview/getInfo",
						param:{
							id:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("topinfo","businessCategoryId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({
								businessCode:options.code
							}),
							url:'common/findCategoryByBusinessCode',
							namefield:"name",
							cachename:_that.categoryCache,
							initload:true,
							searchfield:"q",
							placeholder:"请输入区域",
							noLoadMore:false
						}
					},null,mainData)
					changeEditor("title1", "contractUserId", {
						link: "../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({}),
							namefield: "username",
							url: 'orgInstPosUserOfficial/findSameInstUserListByUserId',
							cachename: _that.userListCache,
							initload: true,
							searchfield: "q",
							placeholder: "请输入姓名"
						}
					},null,mainData)
					changeEditor("title1","contractTemplateTypeCode","data",configSelectData(res[1],"code","dicname"),mainData)
					if (_that.subConfigData["title4"]) {
						let configData = _that.subConfigData["title4"]
						let attr = {}
						if (hasEditor('title4', 'userId', configData)) {
							attr.titleTemplate = '姓名:<%= userName %>'
						}
						if (hasEditor('title4', 'posId', configData)) {
							attr.subtitle1Template = '岗位:<%= posName %>'
						}
						changeEditor('title4', 'objStr', attr, mainData);
					}
					changeEditor("title5", "fileUpload", {
						link: "../../public/uploadFile/uploadFile",
						"linkParam": {
							url: "/tsalesContractReview/uploadFile.do",
							type: "one-on-one",
							filetype:"pdf"
						}
					}, null, mainData)
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[2].value.tsalesContractReview){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value.tsalesContractReview[index])
								_that.$set(_that.formdata,index,res[2].value.tsalesContractReview[index])
							}
						}
						
						let userList = _that._.cloneDeep(res[2].value.tsalesContractReview_users)
						_that.subEditorObj["title4"].workList = userList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr",JSON.stringify(userList))
						_that.$set(_that.formdata,"objStr",JSON.stringify(userList))
						delete res[2].value.tsalesContractReview.userList
						
						_that.$refs.autoform.formdata.fileUpload = res[2].value.fileTable
						_that.$refs.autoform.formdata.fileName = res[2].value.fileTable.fileName
						_that.formdata.fileName = res[2].value.fileTable.fileName
						_that.formdata.fileSaveName = res[2].value.fileTable.fileSaveName
						_that.formdata.fileUrl = res[2].value.fileTable.fileSaveName
						_that.getDir() //获取保存文件夹
						
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
							_that.initPosList(_that.$refs.autoform.formdata.contractUserId)
						})
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				if (_that.files && _that.files.length > 1 && _that.edit !== "true") {
					_that.$refs.autoform.formdata.fileName = _that.files.length + ("个文件")
					_that.$refs.autoform.formdata.fileList = _that.files
				} else {
					if(_that.fileName){
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"fileName",_that.fileName)
						_that.$set(_that.formdata,"fileName",_that.fileName)
					}
					if(_that.files){
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"fileList",_that.files)
						_that.$set(_that.formdata,"fileList",_that.files)
					}
					if(_that.files){
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"fileUpload", _that.files)
						_that.$set(_that.formdata,"fileUpload", _that.files)
					}
					if(_that.fileSaveName){
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"fileUrl", _that.fileSaveName)
						_that.$set(_that.formdata,"fileUrl", _that.fileSaveName)
					}
					if(_that.fileSaveName){
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"fileSaveName",_that.fileSaveName)
						_that.$set(_that.formdata,"fileSaveName", _that.fileSaveName)
					}
				}
				for(let fieldId in _that.subEditorObj){
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key:getWorkCacheKey(fieldId),
						success:(res)=>{
							if(res.data){
								editor.workList = res.data
								uni.removeStorage({
									key:getWorkCacheKey(fieldId)
								})
							}
						}
					})
				}
				uni.getStorage({
					key:_that.categoryCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.businessCategoryId = d.id
							formdata.cateGoryName = d.name
							uni.removeStorage({
								key:_that.categoryCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.userListCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.contractUserId = d.id
							formdata.contractUserName = d.username
							_that.initPosList(d.id)
							uni.removeStorage({
								key:_that.userListCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style></style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
